package textchorus;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import glguerin.narrator.Utterance;
import glguerin.narrator.imp.macosx.NSNarrator;

public class Voices implements Runnable {
	private PSketch p;
	private NSNarrator[] nsn = new NSNarrator[6];
	private Utterance ut;

	private volatile Thread myThread;

	private boolean isPhonemeSpeaking = false;
	private boolean waitEnd = false;
	private String sqStr;

	private enum Voice {
		Alex, Kathy, Vicki, Bruce, Victoria;
	};

	public Voices(PSketch p) {
		this.p = p;

		for (int i = 0; i < Voice.values().length; i++) {
			nsn[i] = new NSNarrator();
		}

		myThread = new Thread(this);
		myThread.start();
	}

	public synchronized void run() {
		while (true) {

			if (!isPhonemeSpeaking) {
				// check if normal chorus is speaking
				ut = getNsn()[0].speaking();
				if (ut == null) {

					if (waitEnd) {
						try {
							Thread.sleep(5000);
						} catch (InterruptedException e) {
//							e.printStackTrace();
						}

						p.setFadeFlag(true);
						waitEnd = false;
						p.ww.doNextHop();
					}
				}
			}
		}
	}

	public void speak(String str) {
		waitEnd = false;
		try {
			for (int i = 0; i < Voice.values().length; i++) {
				if (i == 0) {
					nsn[i].speak(Voice.values()[i].toString(),
							"[[volm 0.6 ; pbas 30 ; rate 175; inpt TEXT]] "
									+ str);
				} else {
					nsn[i].speak(Voice.values()[i].toString(),
							"[[volm 0.3 ; rate 175 ; inpt TEXT]]" + str);
				}
			}

			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			waitEnd = true;
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public void speak(Map<String, Vector<String>> words) {
		waitEnd = false;
		isPhonemeSpeaking = true;

		Iterator<String> it = words.keySet().iterator();

		int useVoice = 0;
		while (it.hasNext()) {
			Object key = it.next();
			Vector<String> value = (Vector<String>) words.get(key);

//			System.out.println("key: " + key + ", value: " + value);

			for (int i = 0; i < value.size(); i += 2) {
				String pair;
				int delay;
				if (i < value.size() - 1) {
					pair = value.get(i) + value.get(i + 1);
					delay = 200;
				} else {
					pair = value.get(i);
					delay = 250;
				}

				try {
					nsn[useVoice].speak(Voice.values()[useVoice].toString(),
							"[[volm 0.8 ; rate 180 ; inpt PHON]]" + pair);

					useVoice++;
					if (useVoice > Voice.values().length - 1)
						useVoice = 0;

					try {
						Thread.sleep(delay);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}

		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		waitEnd = true;
		isPhonemeSpeaking = false;

		speak(sqStr);
	}

	public NSNarrator[] getNsn() {
		return nsn;
	}

	public boolean isSpeaking() {
		return isPhonemeSpeaking;
	}

	public void prep(String s) {
		sqStr = s;
	}

}
